package com.zzw.springboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.springboot.bean.Supplier;
import com.zzw.springboot.bean.SysUser;
import com.zzw.springboot.mapper.SupplierMapper;
import com.zzw.springboot.mapper.SysUserMapper;
import com.zzw.springboot.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 供应商服务实现类
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    @Transactional
    public boolean addSupplier(Supplier supplier) {
        // 检查供应商编码是否已存在
        if (checkSupplierCodeExists(supplier.getSupplierCode(), null)) {
            return false;
        }

        // 检查供应商名称是否已存在
        if (checkSupplierNameExists(supplier.getSupplierName(), null)) {
            return false;
        }

        // 设置默认值
        if (supplier.getLevel() == null) {
            supplier.setLevel("3"); // 默认为一般供应商
        }
        if (supplier.getStatus() == null) {
            supplier.setStatus(1); // 默认启用
        }

        return supplierMapper.insertSupplier(supplier) > 0;
    }

    @Override
    @Transactional
    public boolean updateSupplier(Supplier supplier) {
        // 检查供应商编码是否已存在（排除自身）
        if (checkSupplierCodeExists(supplier.getSupplierCode(), supplier.getSupplierId())) {
            return false;
        }

        // 检查供应商名称是否已存在（排除自身）
        if (checkSupplierNameExists(supplier.getSupplierName(), supplier.getSupplierId())) {
            return false;
        }

        return supplierMapper.updateSupplier(supplier) > 0;
    }

    @Override
    @Transactional
    public boolean deleteSupplier(Integer supplierId) {
        // 可以在此添加业务验证，如检查是否有关联订单等
        return supplierMapper.deleteSupplier(supplierId) > 0;
    }

    @Override
    @Transactional
    public boolean deleteSupplierBatch(Integer[] supplierIds) {
        return supplierMapper.deleteSupplierBatch(supplierIds) > 0;
    }

    @Override
    public Supplier getSupplierById(Integer supplierId) {
        return supplierMapper.selectSupplierById(supplierId);
    }

    @Override
    public List<Supplier> getSupplierList(Supplier supplier) {
        return supplierMapper.selectSupplierList(supplier);
    }

    @Override
    public List<Supplier> getAllSuppliers() {
        return supplierMapper.selectAllSuppliers();
    }

    @Override
    public boolean checkSupplierCodeExists(String supplierCode, Integer supplierId) {
        return supplierMapper.checkSupplierCodeExists(supplierCode, supplierId) > 0;
    }

    @Override
    public boolean checkSupplierNameExists(String supplierName, Integer supplierId) {
        return supplierMapper.checkSupplierNameExists(supplierName, supplierId) > 0;
    }

    @Override
    @Transactional
    public void updateSupplierScore(Integer supplierId) {
        // 获取供应商信息
        Supplier supplier = supplierMapper.selectSupplierById(supplierId);
        if (supplier != null) {
            // 计算综合评分并确定等级
            BigDecimal totalScore = calculateTotalScore(supplier);
            supplier.setTotalScore(totalScore);

            // 确定供应商等级
            String level = determineSupplierLevel(supplier);
            supplier.setLevel(level);

            // 更新供应商
            supplierMapper.updateSupplier(supplier);
        }
    }

    @Override
    public String determineSupplierLevel(Supplier supplier) {
        BigDecimal score = supplier.getTotalScore();
        if (score == null) {
            score = calculateTotalScore(supplier);
        }

        if (score.compareTo(new BigDecimal("9.0")) >= 0) {
            return "S"; // 战略供应商
        } else if (score.compareTo(new BigDecimal("8.0")) >= 0) {
            return "A"; // 优质供应商
        } else if (score.compareTo(new BigDecimal("7.0")) >= 0) {
            return "B"; // 良好供应商
        } else if (score.compareTo(new BigDecimal("6.0")) >= 0) {
            return "C"; // 一般供应商
        } else {
            return "D"; // 待观察供应商
        }
    }

    // 辅助方法 - 计算综合得分
    private BigDecimal calculateTotalScore(Supplier supplier) {
        BigDecimal qualityScore = supplier.getQualityScore() != null ?
                supplier.getQualityScore() : new BigDecimal("5.0");
        BigDecimal deliveryScore = supplier.getDeliveryScore() != null ?
                supplier.getDeliveryScore() : new BigDecimal("5.0");
        BigDecimal priceScore = supplier.getPriceScore() != null ?
                supplier.getPriceScore() : new BigDecimal("5.0");
        BigDecimal serviceScore = supplier.getServiceScore() != null ?
                supplier.getServiceScore() : new BigDecimal("5.0");

        // 计算总分 (质量+交付+价格+服务)/4
        return qualityScore.add(deliveryScore).add(priceScore).add(serviceScore)
                .divide(new BigDecimal("4"), 1, BigDecimal.ROUND_HALF_UP);
    }
}
